var Grid=function(leftMargin,topMargin){
    this.gridElements=new Array();
    this.leftMargin=leftMargin;
    this.topMargin=topMargin;
    this.mode=false;
    this.images=new Array("enemy3.png","enemy2.png","enemy2.png","enemy1.png","enemy1.png");
    this.rowImages = 13;
    this.step=2;
    this.hSpace=55;
    this.vSpace=40;
    this.xDir=true;
    this.maxX=0;
    this.maxY=0;
    this.minX=0;
    this.isEnded=0;
    this.speedRaise=1;
    this.counter=0;
    this.killedEnemiesCounter=0;
    
    this.load=function(){
        var x=this.leftMargin;
        var y=this.topMargin;

        for (var imageIndex=0; imageIndex<this.images.length; imageIndex++){
            this.gridElements[imageIndex]=new Array();
            for (var loadedImages=0; loadedImages<this.rowImages; loadedImages++){
                var enemy=new Enemy(x,y,this.images[imageIndex],false);
                if(imageIndex==0){
                    enemy.type=1;
                }
                else if(imageIndex==1 ||imageIndex==2){
                    enemy.type=2;
                }
                else if(imageIndex==3 || imageIndex==4){
                    enemy.type=3;
                }
                this.gridElements[imageIndex][loadedImages]=enemy;
                x=x+this.hSpace;
            }
            x=this.leftMargin;
            y=y+this.vSpace;
        }
        spaceObj.loadImage();
        this.loadImages();
    }
    
    this.loadImages=function(){
    
        for(var rowIndex=0; rowIndex<this.images.length;rowIndex++){
            for(var colIndex=0;colIndex<this.rowImages;colIndex++){
                this.gridElements[rowIndex][colIndex].loadImage();
            }
        }
    }
    
    this.update=function(){
        if(this.counter==5){
            this.toggleSheet();
            this.counter=0;
        }
        
        this.calculateMargins();
        this.setNewMargins();
        if(this.isEnded === 1){
            return false;
        }else{
            this.render();
        }
        
        if(spaceObj.bullet){
            var isExist=spaceObj.bullet.update();
            if(!isExist){
                spaceObj.bullet=null;
            }
        }
    }
    
    this.toggleSheet=function(){  
        this.mode=!this.mode;
    }
    
    this.render=function(){
        console.log(this.killedEnemiesCounter);
        if(this.killedEnemiesCounter>=this.images.length*this.rowImages){
            
            var imgGameOver = new Image();
            imgGameOver.onload = function() {
                spaceContext.drawImage(imgGameOver,450, 150, 400, 300 );
            };
            imgGameOver.src = 'Images/500.jpg'; 
        }
        var x=this.leftMargin;
        var y=this.topMargin;
        this.counter++;
        for (var imageIndex=0; imageIndex<this.images.length; imageIndex++){
            for (var loadedImages=0; loadedImages<this.rowImages; loadedImages++){
                this.gridElements[imageIndex][loadedImages].update(x,y,this.mode);
                x=x+this.hSpace;
            }
            x=this.leftMargin;
            y=y+this.vSpace;
        }
        context.clearRect(0,60,canvas.width,canvas.height-160);
        spaceContext.clearRect(0,0,spaceCanvas.width,spaceCanvas.height);
        this.loadImages();
    }
       
    this.calculateMargins=function(){
        this.maxY=this.topMargin+(this.images.length*this.vSpace)+100;
        if(this.xDir){
            this.maxX=this.leftMargin+(this.rowImages*this.hSpace)+this.step;
        }else{
            this.minX=this.leftMargin+this.step;
        }
    }
    
    this.setNewMargins=function(){
        if(this.xDir && this.maxX<=canvas.width){
            this.leftMargin+=this.step;
        }
        else if(!this.xDir && this.minX>=0){
            this.leftMargin+=this.step;
        }else if(this.xDir && this.maxY<canvas.height && this.maxX>=canvas.width){
            this.xDir=!this.xDir;
            this.topMargin+=10;
            this.step=-this.step-this.speedRaise;
        }
        else if(!this.xDir && this.maxY<=canvas.height && this.minX<=0){
            this.topMargin+=10;
            this.xDir=!this.xDir;
            this.step=-this.step+this.speedRaise;
        }else if(this.maxY>=canvas.height) {
            this.isEnded=1;
        }
    }    
 
}
